<template>

  <div class="login">


  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <div class="c_logo">
      <span>猪蛛科技后台</span>

    </div>
  <el-form-item label="账号" prop="user">
    <el-input type="text" v-model="ruleForm.user" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
  </el-form-item>
  <!-- <el-form-item
      prop="email"
      label="邮箱"
      :rules="[
        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
      ]"
    >
      <el-input v-model="ruleForm.email"></el-input>
    </el-form-item> -->

    <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</div>
</template>
<script>
  // import vuecookies from 'vue-cookies';
  // Vue.use(vuecookies);
  //import { Message } from 'element-ui';
  export default {
    data() {
      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('年龄不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value < 18) {
              callback(new Error('必须年满18岁'));
            } else {
              callback();
            }
          }
        }, 1000);
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        }  else {
          callback();
        }
      };
      return {
        ruleForm: {
          pass: '',
          user: '',
          age: '',
          email: '',

        },
        rules: {
          user: [
            { validator: validatePass, trigger: 'blur' }
          ],
          pass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          age: [
            { validator: checkAge, trigger: 'blur' }
          ]
        }
      };
    },


    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          var ss
           console.log(this.$refs[formName]);
           console.log(this.$refs[formName].validate());
          if (valid) {
            console.log(this.$refs[formName]);
            this.$axios.post(process.env.API_HOST+'/houtai/loging/login', {user: this.ruleForm.user, pass: this.ruleForm.pass})
                 .then(res => {
                if(res.data.code===1&& res.status===200){
                  console.log(res.data.data.username);

                  //loclStorage.setItem("token",res.data.data.username);
                 // setCookie("token",res.data.data.username);
                  this.$vuecookies.set("token",res.data.data.username,"0");

                  this.$router.push('/');

                }else{
                  console.log(res.data);
                     this.open3(res.data.msg);

                }

                 }, res => {
    // 错误回调
                       console.log(res);



                 })





          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
        open3(msg) {
               this.$message({
                 message: msg,
                 type: 'warning'
               });
             },
    }
  }
</script>

<style>
  .login{

    width: 360px;
    height: 360px;
     display: flex;
     margin: 150px auto;
     border-radius: 15px;
     border: 0.0625rem solid #B3C0D1 ;
     text-align: center;

  }
  .c_logo{
    width: 100%;
    height: 50px;
    background-image:url(../../../static/img/logo.png) !important;
    background-repeat: no-repeat;
    margin-bottom: 50px;
  }
  .c_logo span{
    float: right;
    font-size: 26px;
    font-weight: bold;
    line-height: 50px;
    }
</style>
